package com.woniu.microuserserver.controller;

import com.woniu.microcommonsserver.entity.BuildInfoEntity;
import com.woniu.microcommonsserver.entity.OrgInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microuserserver.bean.DictBean;
import com.woniu.microuserserver.feignApi.ProFeignApi;
import com.woniu.microuserserver.service.OrgService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/org")
@RestController
public class OrgController {
    @Resource
    private ProFeignApi proFeignApi;
    @Resource
    private OrgService orgService;

    //查询远程全部项目的信息
    @RequestMapping("/getpros")
    public ResponseMsg findPros(){
        ResponseMsg res = proFeignApi.findByRank();
        return res;
    }

    @RequestMapping("/addorg")
    public ResponseMsg addorg(@RequestBody OrgInfoEntity orgInfoEntity){
        System.out.println("插入前:"+orgInfoEntity);
        //1添加一个部门。并返回它的主键值
        int i = orgService.addOrg(orgInfoEntity);
        System.out.println("插入后:"+orgInfoEntity);
        //2向中间表添加部门与项目之间的关系
        for (int j = 0; j <orgInfoEntity.getPros().length ; j++) {
            orgService.addOrgPro_id(orgInfoEntity.getId().intValue(), orgInfoEntity.getPros()[j]);
        }
        ResponseMsg msg = new ResponseMsg();
        msg.setCode(200);
        msg.setMsg("添加成功！");
        return msg;
    }

    // 根据部门名称修改部门信息
    @RequestMapping("/updateorg")
    public ResponseMsg updateorg(@RequestBody OrgInfoEntity orgInfoEntity){
        System.out.println(orgInfoEntity);
        //修改部门表
        //修改中间表
        return null;
    }
    //删除部门
    @RequestMapping("/delorg")
    public ResponseMsg delorg(int orgid){
        System.out.println("部门Id="+orgid);
        //判断该部门是否有绑定用户
        //若有删除该部门
        //若无返回提示信息
        return null;
    }
    //根据部门主键查找部门信息
    @RequestMapping("/searchbyid/{orgid}")
    public ResponseMsg searchbyid(@PathVariable int orgid){
        System.out.println(orgid);
        OrgInfoEntity entity = orgService.searchbyid(orgid);
        System.out.println(entity);
        return ResponseMsg.builder().code(200).data(entity).msg("查找成功").build();
    }

    //查询所有部门的id和name
    @RequestMapping("/searchAllOrg")
    public ResponseMsg searchAllOrg(){
        List<OrgInfoEntity> list = orgService.searchAllOrg();
        return ResponseMsg.builder().msg("查询成功").code(200).data(list).build();
    }

    //新加字典
    @RequestMapping("/addDict")
    public ResponseMsg addDict(@RequestBody DictBean dictBean){
        orgService.addDict(dictBean);
        return ResponseMsg.builder().code(200).msg("ok").build();
    }

    //查询字典
    @RequestMapping("/findDict")
    public ResponseMsg findDict(){
        List<DictBean> sd = orgService.sd();
        return ResponseMsg.builder().code(200).msg("ok").data(sd).build();
    }
    //删除字典
    @RequestMapping("/delDict/{id}")
    public ResponseMsg delDict(@PathVariable int id){
        orgService.delDict(id);
        return ResponseMsg.builder().code(200).msg("ok").build();
    }
}
